package Test03;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

public class Dom4jDemo {
	private static Document document;

	public static void main(String[] args) {
		loadXml();
//	 showInfo();
//		 add();
//		update();
		delete();
	}

	public static void delete() {
		// 获取XML的根节点
		Element root = document.getRootElement();
		// 得到根节点下的子节点
		Iterator<Element> iterator = root.elementIterator();
		// 循环所有student节点
		while (iterator.hasNext()) {
			// 取出当前student节点
			Element element = iterator.next();
			// 判断当前循环的节点的id属性值是否是"3"
			if ("小米".equals(element.attributeValue("name"))) {
				element.getParent().remove(element);
			}
		}
		saveXml();
	}

	public static void update() {
		// 获取XML的根节点
		Element root = document.getRootElement();
		Iterator<Element> iterator = root.elementIterator();
		// 循环所有student节点
		while (iterator.hasNext()) {
			// 取出当前student节点
			Element element = iterator.next();
			// 判断当前循环的节点的id属性值是否是"3"
			if ("小米".equals(element.attributeValue("name"))) {
				// 循环当前student节点下的所有子节点
				Iterator<Element> iter = element.elementIterator();
				while (iter.hasNext()) {
					Element ele = iter.next();
					// 如果节点名称是score
					if ("type".equals(ele.getName())) {
						ele.setText("4999");
					}
				}
			}
		}
		saveXml();

	}

	private static void saveXml() {
		try {
			OutputFormat format = OutputFormat.createPrettyPrint();
			format.setEncoding("utf-8"); // 指定XML编码
			XMLWriter writer;
			writer = new XMLWriter(new FileWriter("resources/Phones.xml"), format);
			writer.write(document);
			writer.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private static void add() {
		// 获取根节点
		Element root = document.getRootElement();
		// 添加子节点
		Element mobile = root.addElement("mobile");
		mobile.addAttribute("name", "小米");
		// student节点添加子节点
		Element name = mobile.addElement("name");
		name.setText("小米10");
		Element except = mobile.addElement("price");
		except.setText("4999");
		Element score = mobile.addElement("type");
		score.setText("5G");
		// 保存文件
		saveXml();
	}

	private static void showInfo() {
		// 获取根节点
		Element root = document.getRootElement();
		// 得到该节点下的所有子节点
		Iterator<Element> iterator = root.elementIterator();
		// 循环子节点
		while (iterator.hasNext()) {
			// 得到当前迭代的元素
			Element element = iterator.next();
			// 根据属性名称获取属性值
			System.out.println("型号:" + element.attributeValue("name"));
			// 获取student节点下的子节点
			Iterator<Element> iter = element.elementIterator();
			while (iter.hasNext()) {
				Element ele = iter.next();
				System.out.println(ele.getText());
			}
		}
	}

	// 解析Xml文件,转换成Document对象
	private static void loadXml() {
		SAXReader saxReader = new SAXReader();
		try {
			document = saxReader.read(new File("resources/Phones.xml"));
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}